package com.spic.business.train.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.train.plan.domain.TrainPlan;
import com.spic.business.train.plan.domain.TrainPlanShift;
import com.spic.business.train.plan.domain.TrainPlanStaff;
import com.spic.business.train.plan.mapper.TrainPlanMapper;
import com.spic.business.train.plan.service.ITrainPlanService;
import com.spic.business.train.plan.service.ITrainPlanStatusService;
import com.spic.business.train.trainInfo.dto.TrainCourseWareDTO;
import com.spic.business.train.trainInfo.service.ITrainInfoService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.UUIDUtil;
import com.spic.common.security.utils.cache.DeptCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 培训计划Service业务层处理
 *
 * @author spic
 * @date 2021-10-15
 */
@Service
public class TrainPlanServiceImpl implements ITrainPlanService, ITrainPlanStatusService
{
    @Autowired
    private TrainPlanMapper trainPlanMapper;
    @Autowired
    private ITrainInfoService  trainInfoService;

    /**
     * 查询培训计划
     *
     * @param id 培训计划主键
     * @return 培训计划
     */
    @Override
    public TrainPlan selectTrainPlanById(String id)
    {
        TrainPlan plan = trainPlanMapper.selectTrainPlanById(id);
        plan.setTrainPlanStaffs(trainPlanMapper.selectTrainPlanStaff(id));
        plan.getTrainPlanShiftList().forEach(TrainPlanShift::transCode);
        return plan;
    }

    /**
     * 查询培训计划列表
     *
     * @param trainPlan 培训计划
     * @return 培训计划
     */
    @Override
    @BusinessDataScope(menuPath = "business/train/plan/index")
    public List<TrainPlan> selectTrainPlanList(TrainPlan trainPlan)
    {
        List<TrainPlan> trainPlanList = trainPlanMapper.selectTrainPlanList(trainPlan);
            trainPlanList.forEach(plan -> {
                try {
                    plan.setDeptName(DeptCacheUtils.getValue(plan.getDeptId()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        return trainPlanList;
    }
    /**
     * 新增培训计划
     *
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTrainPlan(TrainPlan trainPlan)
    {
        trainPlan.setCreateTime(DateUtils.getNowDate());
        trainPlan.setId(UUIDUtil.createUUID());
        if(CollectionUtils.isNotEmpty(trainPlan.getTrainCoursewareList())) {
            String trainCoursewareIds = trainPlan.getTrainCoursewareList().stream().map(TrainCourseWareDTO::getTrainCoursewareId).collect(Collectors.joining(","));
            trainPlan.setTrainCoursewareId(trainCoursewareIds);
        }
        int rows = trainPlanMapper.insertTrainPlan(trainPlan);
        insertTrainPlanShift(trainPlan);
        return rows;
    }

    /**
     * 修改培训计划
     *
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTrainPlan(TrainPlan trainPlan)
    {
        trainPlanMapper.deleteTrainPlanShiftByTrainPlanId(trainPlan.getId());
        trainPlanMapper.deleteTrainPlanStaffByTrainPlanId(trainPlan.getId());
        insertTrainPlanShift(trainPlan);
        int row = trainPlanMapper.updateTrainPlan(trainPlan);
        trainInfoService.updateTrainInfo(trainPlan.toTrainInfoDto());
        return row;
    }

    /**
     * 批量删除培训计划
     *
     * @param ids 需要删除的培训计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTrainPlanByIds(String[] ids)
    {
        trainPlanMapper.deleteTrainPlanShiftByIds(ids);
        trainPlanMapper.deleteTrainPlanStaffByTrain(ids);
        trainInfoService.deleteTrainInfoByIds(ids);
        int rows = trainPlanMapper.deleteTrainPlanByIds(ids);
        return rows;
    }

    /**
     * 删除培训计划信息
     *
     * @param id 培训计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTrainPlanById(String id)
    {
        trainPlanMapper.deleteTrainPlanShiftByTrainPlanId(id);
        trainPlanMapper.deleteTrainPlanStaffByTrainPlanId(id);
        int row = trainPlanMapper.deleteTrainPlanById(id);
        trainInfoService.deleteTrainInfoByIds(new String[]{id});
        return row;
    }

    @Override
    public List<TrainPlan> selectTrainPlanList(List<String> planStandingBookIds) {
        return trainPlanMapper.selectList(
                new LambdaQueryWrapper<TrainPlan>()
                        .in(TrainPlan::getPlanStandingBookId, planStandingBookIds)
        );
    }

    /**
     * 新增培训方案-培训班数信息
     *
     * @param trainPlan 培训计划对象
     */
    public void insertTrainPlanShift(TrainPlan trainPlan)
    {
        List<TrainPlanShift> trainPlanShiftList = trainPlan.getTrainPlanShiftList();
        String id = trainPlan.getId();
        if (CollectionUtils.isNotEmpty(trainPlanShiftList))
        {
            for (TrainPlanShift trainPlanShift : trainPlanShiftList)
            {
                trainPlanShift.setId(UUIDUtil.createUUID());
                trainPlanShift.setTrainPlanId(id);
            }
            trainPlanMapper.batchTrainPlanShift(trainPlanShiftList);
        }
        List<TrainPlanStaff> planStaffList = trainPlan.getTrainPlanStaffs();
        if(CollectionUtils.isNotEmpty(planStaffList)){
            for (TrainPlanStaff staff : planStaffList) {
                staff.setId(UUIDUtil.createUUID());
                staff.setTrainPlanId(id);
            }
            trainPlanMapper.batchTrainPlanStaff(planStaffList);
        }
        trainInfoService.insertTrainInfo(trainPlan.toTrainInfoDto());
    }

    @Override
    public void updatePlanStatus(String planId, Status status) {
        trainPlanMapper.updateStatus(planId, status.ordinal());
    }
}
